﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC;
using YArchitech.MEP;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LaySprinklersSector : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			SectorLaySprinkler sectorLaySprinkler = null;
			Document document = null;
			Dictionary<int, IList<RoutingPreferenceRule>> dictionary = null;
			int num = -1;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException();
				}
				UIApplication application = commandData.Application;
				UIDocument activeUIDocument = application.ActiveUIDocument;
				document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
				{
					(ViewType)1,
					(ViewType)2
				}))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					FormSectorLaySprinkler formSectorLaySprinkler = new FormSectorLaySprinkler(document);
					if (formSectorLaySprinkler.ShowDialog() == DialogResult.Yes)
					{
						List<List<XYZ>> list = new List<List<XYZ>>();
						sectorLaySprinkler = new SectorLaySprinkler(document);
						FamilySymbol familySymbol = null;
						new AuxiliaryLineOperate();
						List<List<FamilyInstance>> list2 = new List<List<FamilyInstance>>();
						CreateProtectRange createProtectRange = new CreateProtectRange();
						if (formSectorLaySprinkler.userSettingInfo.LayStyle == "Sector")
						{
							using (SprinklerSectorJig sprinklerSectorJig = new SprinklerSectorJig(activeUIDocument))
							{
								if (!sprinklerSectorJig.SectorPoint(sectorLaySprinkler, formSectorLaySprinkler.userSettingInfo) || !sprinklerSectorJig.underPoint())
								{
									return Autodesk.Revit.UI.Result.Cancelled;
								}
								list = sprinklerSectorJig.xyzList;
							}
							transaction = new Transaction(document, "布置喷头");
							transaction.Start();
							if (formSectorLaySprinkler.CurrentBtn == 1)
							{
								familySymbol = formSectorLaySprinkler.Fsymbol;
							}
							else
							{
								familySymbol = sectorLaySprinkler.GetSprinklerSymbol(formSectorLaySprinkler.userSettingInfo.FamilyID, formSectorLaySprinkler.userSettingInfo.FamilySymbolName);
							}
							if (familySymbol == null)
							{
								transaction.RollBack();
								YJKMessageBox.Show("获取喷头失败");
								return Autodesk.Revit.UI.Result.Cancelled;
							}
							double num2 = formSectorLaySprinkler.userSettingInfo.OffSet * 1000.0 / 304.8;
							foreach (List<XYZ> list3 in list)
							{
								List<XYZ> list4 = new List<XYZ>();
								foreach (XYZ xyz in list3)
								{
									list4.Add(new XYZ(xyz.X, xyz.Y, num2));
								}
								List<FamilyInstance> item = sectorLaySprinkler.SectorLaySprinklers(list4, familySymbol);
								list2.Add(item);
							}
							document.Regenerate();
							int num3 = 0;
							if (list2 != null && list2.Count > 0)
							{
								foreach (List<FamilyInstance> list5 in list2)
								{
									foreach (FamilyInstance fi in list5)
									{
										this.SetSprinklerRadius(fi, application, formSectorLaySprinkler.userSettingInfo.Radius * 1000.0 / 304.8, formSectorLaySprinkler.userSettingInfo.KCoffie);
										num3++;
									}
								}
								if (formSectorLaySprinkler.userSettingInfo.DrawRange)
								{
									foreach (List<FamilyInstance> list6 in list2)
									{
										foreach (FamilyInstance item2 in list6)
										{
											List<Element> collection = new List<Element>
											{
												item2
											};
											createProtectRange.CreatActiveViewDetailCurve(document, collection, HyRangeCheckMajor.HyGPS);
										}
									}
								}
								if (formSectorLaySprinkler.userSettingInfo.DrawLength > 0.03)
								{
									HYPipeConnect hypipeConnect = new HYPipeConnect();
									foreach (List<FamilyInstance> source in list2)
									{
										if (formSectorLaySprinkler.userSettingInfo.IsConn == 0)
										{
											hypipeConnect.ConnSprinklersInALine(document, source.ToList<Element>(), formSectorLaySprinkler.userSettingInfo.DrawLength, formSectorLaySprinkler.userSettingInfo.PipeType);
										}
									}
								}
							}
							if (num3 > 0)
							{
								YJKMessageBox.Show("成功布置" + num3 + "个喷头。");
							}
							else
							{
								YJKMessageBox.Show("布置喷头失败。");
							}
							transaction.Commit();
						}
						else
						{
							new List<XYZ>();
							List<XYZ> list7 = new List<XYZ>();
							using (SprinklerArcJig sprinklerArcJig = new SprinklerArcJig(activeUIDocument))
							{
								if (!sprinklerArcJig.ArcPoint(sectorLaySprinkler, formSectorLaySprinkler.userSettingInfo))
								{
									return Autodesk.Revit.UI.Result.Cancelled;
								}
								List<XYZ> xyzList = sprinklerArcJig.xyzList;
								double num4 = formSectorLaySprinkler.userSettingInfo.OffSet * 1000.0 / 304.8;
								foreach (XYZ xyz2 in xyzList)
								{
									list7.Add(new XYZ(xyz2.X, xyz2.Y, num4));
								}
							}
							transaction = new Transaction(document, "弧线布置喷头");
							transaction.Start();
							if (formSectorLaySprinkler.CurrentBtn == 1)
							{
								familySymbol = formSectorLaySprinkler.Fsymbol;
							}
							else
							{
								familySymbol = sectorLaySprinkler.GetSprinklerSymbol(formSectorLaySprinkler.userSettingInfo.FamilyID, formSectorLaySprinkler.userSettingInfo.FamilySymbolName);
							}
							if (familySymbol == null)
							{
								YJKMessageBox.Show("获取喷头失败");
								return Autodesk.Revit.UI.Result.Cancelled;
							}
							List<FamilyInstance> list8 = sectorLaySprinkler.SectorLaySprinklers(list7, familySymbol);
							document.Regenerate();
							if (list8 != null && list8.Count > 0)
							{
								for (int i = 0; i < list8.Count; i++)
								{
									this.SetSprinklerRadius(list8[i], application, formSectorLaySprinkler.userSettingInfo.Radius * 1000.0 / 304.8, formSectorLaySprinkler.userSettingInfo.KCoffie);
								}
								if (formSectorLaySprinkler.userSettingInfo.DrawRange)
								{
									for (int j = 0; j < list8.Count; j++)
									{
										List<Element> collection2 = new List<Element>
										{
											list8[j]
										};
										createProtectRange.CreatActiveViewDetailCurve(document, collection2, HyRangeCheckMajor.HyGPS);
									}
								}
								YJKMessageBox.Show("成功布置" + list8.Count + "个喷头。");
							}
							else
							{
								YJKMessageBox.Show("布置喷头失败。");
							}
							transaction.Commit();
						}
					}
					else
					{
						formSectorLaySprinkler.Dispose();
					}
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.EndTransation(transaction, false);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				this.EndTransation(transaction, true);
				if (dictionary != null && num >= 0)
				{
					PipeConfigurationManagement.SetRules(num, commandData.Application.ActiveUIDocument.Document, dictionary, true);
				}
			}
			return result;
		}

		private void EndTransation(Transaction tran, bool beSure)
		{
			if (tran != null)
			{
				TransactionStatus status = tran.GetStatus();
				if ((int)status == 1)
				{
					if (beSure)
					{
						tran.Commit();
						return;
					}
					tran.RollBack();
					return;
				}
				else if ((int)status == 5)
				{
					tran.RollBack();
				}
			}
		}

		private void SetSprinklerRadius(FamilyInstance fi, UIApplication uiApp, double radius, double kCoff)
		{
			HYParametersSetting hyparametersSetting = new HYParametersSetting(uiApp);
			Parameter parameter = fi.GetParameter("喷淋半径");
			if (parameter == null)
			{
				hyparametersSetting.SetSprinklersRudioShareParam();
				parameter = fi.GetParameter("喷淋半径");
			}
			if (parameter != null)
			{
				parameter.Set(radius);
			}
			Parameter parameter2 = fi.Symbol.get_Parameter(BuiltInParameter.RBS_FP_SPRINKLER_K_FACTOR_PARAM);
			if (parameter2 != null)
			{
				parameter2.Set(kCoff);
			}
		}
	}
}
